package cn.tedu.ssm.service;

import cn.tedu.ssm.mapper.UserMapper;
import cn.tedu.ssm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper mapper;

    public Map<String,Object> list(Integer page,Integer size){
        int limit = size == null || size < 1 ? 10 : size;
        page = page == null || page < 1 ? 1 : page;
        int offset  =(page -1 ) * limit;

        List<User> users = mapper.find(offset, limit);
        Long count = mapper.count();
        Map<String,Object> map =new HashMap<>();
        map.put("users",users);
        map.put("count",count);
        return  map;
    }
    public Long checkUsernameAndPassword(String username, String password) {
        if (!mapper.exists(username)) {
            // 账号不存在，返回 -1
            return -1L;
        }
        // 根据账号和密码查找其 ID
        Long id = mapper.findIdByUsernameAndPassword(username, password);
        if (id == null || id < 1) {
            // 账号和密码不匹配，返回 -2
            return -2L;
        }
        if (mapper.isDeleted(id)) {
            // 账号已经被删除，返回 -3
            return -3L;
        }
        if (mapper.isDisabled(id)) {
            // 账号已经被禁用，返回 -4
            return -4L;
        }
        return id;
    }
    public Map<String, Object> getUserInfo(Long id) {

        HashMap<String, Object> map = new HashMap<>();

        if (!mapper.existsById(id)) {
            return null;
        }

        User user = mapper.userInfo(id);

        map.put("id", user.getId());
        map.put("disabled", user.getDisabled());
        map.put("deleted", user.getDeleted());
        map.put("name", user.getName());
        map.put("username", user.getUsername());

        return map;
    }
}
